package com.zwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.News;
import com.zwz.dao.NewsDao;
import com.zwz.service.NewsInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NewsInfoServiceImpl implements NewsInfoService {

    @Autowired
    private NewsDao newsDao;

    @Override
    public boolean insert(News news) {
        int result = newsDao.insert(news);
        if(result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean deleteById(Integer uid) {
        int result = newsDao.deleteById(uid);

        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean updateById(News userInfo) {
        int result = newsDao.updateById(userInfo);
        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public News getById(Integer nid) {
        News news = newsDao.selectById(nid);
        return news;
    }

    @Override
    public IPage<News> getPage(int currentPage, int pageSize, News news) {
        IPage<News> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<News> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(news.getTitle()), News::getTitle, news.getTitle());
        lqw.like(Strings.isNotEmpty(news.getContents()),News::getContents, news.getContents());

        lqw.orderByDesc(News::getNid);
        newsDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public int countAll() {
        int result = newsDao.countAll();
        return result;
    }
}
